var RemindDetail = function () {
	var editable = function () {

        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                    "lengthMenu": [
                        [10, 15, 20, 50, -1],
                        [10, 15, 20, 50, "All"]
                    ],
                    "displayLength": 10,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                    },
                    "columnDefs": [
						{
						    'sortable': false,
						    "defaultContent": "",
						    "targets": [4]
						}            
                    ],
                    "columns": [
                       { "data": null },
                       { "data": "employeeName" },
                       { "data": "orderId" },
                       { "data": "orderTime" }
                   ],
                   "filter" : false,
                   "sortable" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/remind/queryDetail",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   var mobile = aData.mobile;
                	   nRow.children[1].id = mobile;
                	   nRow.children[2].id = aData.productName;
                	   nRow.children[3].id = aData.endDate.substring(0,10);
                	   $('td:eq(0)', nRow).html('<input name="chkInvestor" onclick="checkone('+ mobile +',this)" type="checkbox" value="'+ mobile +'"/>');
                	   
                	   $('td:eq(4)', nRow).html('<a class="send fa fa-commenting-o fa-lg" title="发送短信" href="javascript:;"></a>');
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"start":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   var form = $("form#queryForm").serializeJSON();
                   	   var js = form.replace('}', ',');
                   	   json = js + json + "}";
                       $.ajax({
                       	   "contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback,
                           "error" : function(){
                        	   window.location.href=ctx + "/employee/login";
                           }
                       });
                   },
                });
                
                // 发送短信
                $('#modelEditable').on("click", "a.send", (function (e) {
                    e.preventDefault();

                    var nRow = $(this).parents('tr')[0];
                    var mobile = nRow.children[1].id;
                    var productName = nRow.children[2].id;
                    var endDate = nRow.children[3].id;
                    
                    var obj = {};
                    obj.mobile = mobile;
                    obj.productName = productName;
                    obj.endDate = endDate;
                    var json = "[" + JSON.stringify(obj) + "]";
                    var message = "确认发送短信“您购买的["+productName+"]基金产品将于"+endDate+"到期，请联系您的专属理财师进行兑付”给投资人吗？";
                    bootbox.dialog({
                        message: message,
                        title: "确认发送兑付短信",
                        buttons: {
                        	default: {
	                            label: "取消",
	                            className: "btn btn-white",
	                            callback: function() {
	                              
	                            }
	                        },
                        	success: {
                            label: "确定",
                            className: "btn btn-primary",
                            callback: function() {
                            	send(json);
                            }
                          }
                        }
                    });
                }));
                
                $('#selectAll').on('click', function(){
                	var tbody = oTable[0].children[1];
                	
                	if (this.checked) { // 选中
                		mobileList=[];//清理，防止混乱
                		$("input[name='chkInvestor']").each(function(){
                			$(this).prop('checked', true);
                			var nRow = $(this).parents('tr')[0];
                			var obj = {};
                            obj.mobile = $(this).val();
                            obj.productName = nRow.children[2].id;
                            obj.endDate = nRow.children[3].id;
                			mobileList.push(obj);
                		});
                	} else { // 反选
                		$("input[name='chkInvestor']").prop("checked", false);
                		mobileList=[];
                	}
                });
                
                $('#sendMessage').click(function(){
                	if (mobileList.length == 0) {
                		bootbox.alert('请选择要发送的投资人。');
                		return;
                	}
                	bootbox.dialog({
                        message: "确认要给勾选的投资人发送即将到期兑付短信？",
                        title: "群发短信提示",
                        buttons: {
                        	default: {
	                            label: "取消",
	                            className: "btn btn-white",
	                            callback: function() {
	                              
	                            }
	                        },
                        	success: {
                            label: "确定",
                            className: "btn btn-primary",
                            callback: function() {
                            	var json = JSON.stringify(mobileList);
                            	send(json);
                            }
                          }
                        }
                    });
                });
                
                $('#queryBtn').click(function(){
                	oTable.fnClearTable();
                });
            }

        };

    }();
    
    function send(json){
    	$.ajax({
        	contentType:"application/json; charset=UTF-8",
        	type:"post",
            url: ctx + "/remind/sendMessage",
            data:json,
            dataType:"json",
            success: function(msg){
            	if (msg.code == 1) {
            		bootbox.alert("短信发送成功！");
            	} else {
            		bootbox.alert("短信发送失败！");
            	}
            },
            error: function (msg) {
            	bootbox.alert(msg.message);
            }
        });
    }
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();
//使用全局方法和变量，不是好的建议，暂时这么办吧
var mobileList = [];
function checkone(mobile, _this){
	if (!_this.checked) { // 取消选中
		$('#selectAll').prop('checked', false);
		var nRow = $(_this).parents('tr')[0];
		var obj = {};
        obj.mobile = $(_this).val();
        obj.productName = nRow.children[2].id;
        obj.endDate = nRow.children[3].id;
        var json = JSON.stringify(obj);
        for (var i = 0; i < mobileList.length; i++) {
        	var t = JSON.stringify(mobileList[i]);
        	if (json == t) {
        		mobileList.splice(i,1);//删除
        	}
        }
	} else { // 单选一个
		var nRow = $(_this).parents('tr')[0];
		var obj = {};
        obj.mobile = $(_this).val();
        obj.productName = nRow.children[2].id;
        obj.endDate = nRow.children[3].id;
        
		mobileList.push(obj);
		var flag = true;
		$("input[name='chkInvestor']").each(function(){ // 遍历所有的checkbox，看是否选中所有了，如果是，将“全选”选上
			if(!$(this).prop('checked')){ // 有没选中的
				$('#selectAll').prop('checked', false);
				flag = false;
				return false;// 相当于break
			}
		});
		if (flag) {
			$('#selectAll').prop('checked', true);
		}
	}
};